//
//  XFPageControllerManager.m
//  Test
//
//  Created by ukongm on 17/5/17.
//  Copyright © 2017年 ukongm. All rights reserved.
//

#import "XFPageControllerManager.h"

@interface XFPageControllerManager ()

/** 复用池 */
@property(nonatomic,strong)NSMutableDictionary* cache;

@end

@implementation XFPageControllerManager
- (NSMutableDictionary *)cache{
    
    if (_cache == nil) {
        _cache = [NSMutableDictionary new];
    }
    return _cache;
}

#pragma mark - 处理方法(私有)

//判断是否达到处理条件
- (void)_examineVC{
    if (!self.vc) {
        [self _exception:@"没有给vc赋值"];
        return;
    }
}

//抛出异常
- (void)_exception:(NSString*)value{
    [NSException raise:@"XFPageControllerManager" format:@"%@",value];
}


//执行代理
- (void)_taskCreateViewController:(UIViewController<XFSubPageControllerDelegate>*)subVC atIndex:(NSInteger)index{
    if ([self.vc respondsToSelector:@selector(createViewController:atIndex:)]) {
        [self.vc createViewController:subVC atIndex:index];
    }
}

//根据index（位置）获取对应的控制器
- (UIViewController<XFSubPageControllerDelegate>*)_subPageForIndex:(NSInteger)index{
    NSString* key = [NSString stringWithFormat:@"%ld",index];
    
    return [self.cache objectForKey:key];
}

//清空视图
- (void)_clear{
    for (UIViewController* vc in self.cache.allValues) {
        [vc.view removeFromSuperview];
        [vc removeFromParentViewController];
    }
    
    [self.cache removeAllObjects];
}

#pragma mark - 自定义事件

- (void)managerForIndex:(NSInteger)index{
    
    NSString* pageName = [self.pages objectAtIndex:index];
    
    //判断是否是字符串
    if ([pageName isKindOfClass:[NSString class]]) {
        //是字符串，开始创建
        UIViewController<XFSubPageControllerDelegate>* subVC = [self _subPageForIndex:index];
        
        if (!subVC) {
            
            Class pageClass = NSClassFromString(pageName);
            
            subVC = [pageClass new];
            
            
            subVC.height = self.vc.scrollView.frame.size.height;
            [self _taskCreateViewController:subVC atIndex:index];
            
            subVC.view.frame = CGRectMake(index * self.vc.scrollView.frame.size.width, subVC.view.frame.origin.y, subVC.view.frame.size.width, subVC.view.frame.size.height);
            
            [self.vc.scrollView addSubview:subVC.view];
            [self.vc addChildViewController:subVC];
            [self.cache setObject:subVC forKey:[NSString stringWithFormat:@"%ld",index]];
        }
        
    }else{
        [self _exception:@"pages中必须是控制器name的字符串形式"];
    }
    
}

- (void)resume{
    [self _examineVC];
    
    [self _clear];
    
    [self managerForIndex:0];
}

- (void)reload{
    [self _examineVC];
    
    for (NSInteger i = 0; i < self.pages.count; i++) {
        
        [self managerForIndex:i];
    }
}


- (void)showForIndex:(NSInteger)index{
    [self showForIndex:index animation:NO];
}

- (void)showForIndex:(NSInteger)index animation:(BOOL)animation{
    [self.vc.scrollView setContentOffset:CGPointMake(self.vc.scrollView.frame.size.width * index, 0) animated:animation];
    
    [self managerForIndex:index];
}

@end
